package crystal.workshop;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;


import javax.media.opengl.GLProfile;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JPanel;
import javax.swing.UIManager;

import crystal.workshop.airss.Airss;
import crystal.workshop.ui.StructuresListTableUI;
import crystal.workshop.ui.renderer.CrystalPanel;

import crystal.renderer.RendererPanel;

public class CrystalWorkshop {
	
	private static final CrystalWorkshopFramework framework = new CrystalWorkshopFramework();  //  @jve:decl-index=0:
	
    /** Frame containing everything. */
    protected final JFrame  jFrame;
    protected JPanel	jContentPane;
    protected StructuresListTableUI strListUI;
    private JMenu jMenu = null;

	private JMenuItem jMenuItem = null;

	private JScrollPane jScrollPane = null;

	private CrystalPanel rendererPanel = null;

	private CrystalWorkshop() {
    	jFrame = createFrame();
		
    	Container       cPane = getJContentPane();
		final JMenuBar	mb = createMenuBar();
        
		strListUI.setStructureListModel(framework.getStructuresList());
		strListUI.setStructureSelectionModel(framework.getStructureSelectionModel());
		
		rendererPanel.setStructureSelectionModel(framework.getStructureSelectionModel());
		
        jFrame.setJMenuBar(mb);
        jFrame.pack();
	}
	
	
    /**
     * Creates the JFrame that will contain everything.
     */
    protected JFrame createFrame() {
    	final JFrame retFrame = new JFrame("CrystalWorkshop");

    	retFrame.setSize(new Dimension(1024, 768));
    	retFrame.setContentPane(getJContentPane());
    	retFrame.setJMenuBar(createMenuBar());
		retFrame.addWindowListener(new WindowAdapter() {
		    public void windowClosing(WindowEvent we) {
		    	jFrame.dispose();
				System.exit(0);
		    }
		});
		return retFrame;
    }

    /**
     * Creates a menu bar.
     */
    protected JMenuBar createMenuBar() {
        JMenu            fileMenu = new JMenu("File"); 
	
        JMenuItem menuItem = new JMenuItem("Exit"); 
        menuItem.setText("Exit");
        menuItem.addActionListener(new ActionListener() {
        	public void actionPerformed(ActionEvent e) {
        		System.exit(0);
        	}
        });
        fileMenu.add(getJMenu());
        fileMenu.addSeparator();
        // Create a menu bar
        fileMenu.add(menuItem);
        JMenuBar        menuBar = new JMenuBar();

        menuBar.add(fileMenu);

        // Create options menu
		JMenu          optionsMenu = new JMenu("Options");

		menuBar.add(optionsMenu);
		return menuBar;
    }

	/**
	 * This method initialises jContentPane	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(new BorderLayout());
			jContentPane.add(getJScrollPane(), BorderLayout.WEST);
			jContentPane.add(getRendererPanel(), BorderLayout.CENTER);
		}
		return jContentPane;
	}
	
	/**
	 * Return the structure list UI.
	 * @return the structure list UI.
	 */
	private StructuresListTableUI getStructuresListUI() {
		if (strListUI == null) {
			try {
				strListUI = new StructuresListTableUI();
				strListUI.setName("strListUI");
				strListUI.setBackground(java.awt.SystemColor.window);
			} catch (java.lang.Throwable ivjExc) {
				//handleException(ivjExc);
			}
		}
		return strListUI;
	}
	
	private void setVisible(boolean b) {
		jFrame.setVisible(b);
	}
	
	/**
	 * This method initializes jMenu	
	 * 	
	 * @return javax.swing.JMenu	
	 */
	private JMenu getJMenu() {
		if (jMenu == null) {
			jMenu = new JMenu();
			jMenu.setText("Open");
			jMenu.add(getJMenuItem());
		}
		return jMenu;
	}


	/**
	 * This method initializes jMenuItem	
	 * 	
	 * @return javax.swing.JMenuItem	
	 */
	private JMenuItem getJMenuItem() {
		if (jMenuItem == null) {
			jMenuItem = new JMenuItem();
			jMenuItem.setText("AIRSS Folder");
			jMenuItem.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
	        		final JFileChooser      fc = new JFileChooser();
	        		fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
	        		final int               result = fc.showOpenDialog(jFrame);

	        		if (result == JFileChooser.APPROVE_OPTION) {
	        			final File newPath = fc.getSelectedFile();
	        			final Airss	airss = new Airss(newPath);
	        			framework.getStructuresList().addAll(airss.getStructures(), airss);
	        		}
				}
			});
		}
		return jMenuItem;
	}


	/**
	 * This method initializes jScrollPane	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getJScrollPane() {
		if (jScrollPane == null) {
			jScrollPane = new JScrollPane();
			jScrollPane.setViewportView(getStructuresListUI());
		}
		return jScrollPane;
	}


	/**
	 * This method initializes rendererPanel	
	 * 	
	 * @return crystal.renderer.RendererPanel	
	 */
	private CrystalPanel getRendererPanel() {
		if (rendererPanel == null) {
			rendererPanel = new CrystalPanel();
			rendererPanel.setPreferredSize(new Dimension(500, 700));
		}
		return rendererPanel;
	}


	/**
	 * @param args
	 */
	public static void main(String[] args) {
		//GLProfile.initSingleton(true);
		try	{
			final String systemLf = UIManager.getSystemLookAndFeelClassName();
			//if(systemLf != "com.sun.java.swing.plaf.gtk.GTKLookAndFeel")
			//	UIManager.setLookAndFeel( systemLf );
		}
		catch( Exception e ) {
			e.printStackTrace();
		}
		CrystalWorkshop workshop = new CrystalWorkshop();
		workshop.setVisible(true);
	}

}  //  @jve:decl-index=0:visual-constraint="10,10"
